package 二叉树最大深度;

/**
 * @author: zhengtao
 * @description: 二叉树的最大深度,层次遍历可以做，这道题使用递归来做
 * @version: 1.0
 * @date: 2022/10/29  17:21
 */
public class Demo104 {
    public int maxDepth(TreeNode root) {
       return getDepth(root,0);

    }
     //递归三部曲
    //1.确定参数与返回值
    private int getDepth(TreeNode root, int i) {
        //2，确定递归终止条件
        if(root==null){
            return i;
        }else {
            //3.递归
            return Math.max(getDepth(root.left,i+1),getDepth(root.right,i+1));
        }
    }

    /**
     * Definition for a binary tree node.
     * */
     public class TreeNode {
     int val;
     TreeNode left;
     TreeNode right;
     TreeNode() {}
     TreeNode(int val) { this.val = val; }
     TreeNode(int val, TreeNode left, TreeNode right) {
     this.val = val;
     this.left = left;
     this.right = right;
     }
     }

}
